﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace UserInterfaceLayer.User
{
    public partial class Person : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            lblFailure.Visible = false;
            lblInvalidInput.Visible = false;
            lblSuccessful.Visible = false;
            lblNewItem.Visible = false;
            lblDeletedItem.Visible = false;
            btnDelete.Visible = false;
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            string strPersonID = Request.QueryString["PersonID"];
            if (strPersonID != "add")
            {
                int PersonID;
                if (!int.TryParse(strPersonID, out PersonID))
                    Response.Redirect("Person.aspx?PersonID=add");
                DataAccessLayer.linqDatabaseDataContext db = new DataAccessLayer.linqDatabaseDataContext();
                DataAccessLayer.Person Person = DataAccessLayer.Person.FindByPersonID_IncludeDeleted(db, PersonID);
                if (Person == null)
                    Response.Redirect("Person.aspx?PersonID=add");
                FromDatabase(Person);
                btnDelete.Visible = true;
                lvHistory.DataSource = BusinessLogicLayer.DataObject.Person.Convert(DataAccessLayer.Person.HistoryToListByPersonID(db, PersonID));
                lvHistory.DataBind();
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                int UserID = (int)Session["UserID"];
                string strPersonID = Request.QueryString["PersonID"];
                if (strPersonID != "add")
                {
                    int PersonID = int.Parse(strPersonID);
                    DataAccessLayer.linqDatabaseDataContext db = new DataAccessLayer.linqDatabaseDataContext();
                    //DataAccessLayer.Person Person = DataAccessLayer.Person.FindByPersonID_IncludeDeleted(db, PersonID);
                    DataAccessLayer.Person NewPerson = new DataAccessLayer.Person();
                    NewPerson.PersonID = PersonID;
                    NewPerson.EditorUserID = UserID;
                    NewPerson.Timestamp = DateTime.Now;
                    ToDatabase(NewPerson);
                    db.Persons.InsertOnSubmit(NewPerson);
                    db.SubmitChanges();
                    lblSuccessful.Visible = true;
                }
                else
                {
                    DataAccessLayer.linqDatabaseDataContext db = new DataAccessLayer.linqDatabaseDataContext();
                    DataAccessLayer.sPerson sPerson = new DataAccessLayer.sPerson();
                    db.sPersons.InsertOnSubmit(sPerson);
                    db.SubmitChanges();
                    DataAccessLayer.Person NewPerson = new DataAccessLayer.Person();
                    NewPerson.PersonID = sPerson.ID;
                    NewPerson.EditorUserID = UserID;
                    NewPerson.Timestamp = DateTime.Now;
                    ToDatabase(NewPerson);
                    db.Persons.InsertOnSubmit(NewPerson);
                    db.SubmitChanges();
                    lblNewItem.Visible = true;
                    btnSave.Visible = false;
                }
            }
            catch (Exception)
            {
                lblFailure.Visible = true;
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                int UserID = (int)Session["UserID"];
                string strPersonID = Request.QueryString["PersonID"];
                int PersonID = int.Parse(strPersonID);
                DataAccessLayer.linqDatabaseDataContext db = new DataAccessLayer.linqDatabaseDataContext();
                //DataAccessLayer.Person Person = DataAccessLayer.Person.FindByPersonID_IncludeDeleted(db, PersonID);
                DataAccessLayer.Person NewPerson = new DataAccessLayer.Person();
                NewPerson.PersonID = PersonID;
                NewPerson.EditorUserID = UserID;
                NewPerson.Timestamp = DateTime.Now;
                NewPerson.IsDeleted = true;
                ToDatabase(NewPerson);
                db.Persons.InsertOnSubmit(NewPerson);
                db.SubmitChanges();
                lblSuccessful.Visible = true;
            }
            catch (Exception)
            {
                lblFailure.Visible = true;
            }
        }

        public void FromDatabase(DataAccessLayer.Person Person)
        {
            txtBirthday.Text = Person.Birthday;
            txtCellNumber.Text = Person.Telephone2;
            txtDescription.Text = Person.Description;
            txtFatherName.Text = Person.FatherName;
            txtFirstName.Text = Person.FirstName;
            txtHomeAddress.Text = Person.HomeAddress;
            txtHomeNumber.Text = Person.Telephone1;
            txtIdentityNumber.Text = Person.IdentityNumber;
            txtJob.Text = Person.Job;
            txtLastName.Text = Person.LastName;
            txtNationalNumber.Text = Person.NationalNumber;
            lblDeletedItem.Visible = Person.IsDeleted;
        }

        public void ToDatabase(DataAccessLayer.Person Person)
        {
            Person.Birthday = txtBirthday.Text;
            Person.Telephone2 = txtCellNumber.Text;
            Person.Description = txtDescription.Text;
            Person.FatherName = txtFatherName.Text;
            Person.FirstName = txtFirstName.Text;
            Person.HomeAddress = txtHomeAddress.Text;
            Person.Telephone1 = txtHomeNumber.Text;
            Person.IdentityNumber = txtIdentityNumber.Text;
            Person.Job = txtJob.Text;
            Person.LastName = txtLastName.Text;
            Person.NationalNumber = txtNationalNumber.Text;
        }
    }
}